package com.ruoyi.web.controller.Gun;

import com.alibaba.fastjson2.JSON;
import com.ruoyi.Gun.domain.Gun;
import com.ruoyi.Gun.service.GunService;
import com.ruoyi.common.annotation.Anonymous;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.redis.RedisCache;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.concurrent.TimeUnit;
    @Anonymous
    @Api(tags = "枪械管理")
    @RestController
    public class GunController {

        @Autowired
        GunService gunService;
        @Autowired
        RedisCache cache;

        @ApiOperation(value = "枪械列表查询")
        @PostMapping("/GunList")
        public AjaxResult GunList(@RequestBody Gun gun) {
            List<Gun> resultList = gunService.GunList(gun);
            for (int i = 0; i < resultList.size(); i++) {
                String jsonStr = JSON.toJSONString(resultList.get(i));
                int id = resultList.get(i).getId();
                cache.setCacheObject(String.valueOf(id), jsonStr, 20, TimeUnit.SECONDS);
            }
            return AjaxResult.success(resultList);
        }

        @ApiOperation(value = "获取枪械详情")
        @ApiImplicitParam(name = "name", value = "枪械名称", dataType = "string", dataTypeClass = String.class)
        @GetMapping("selectGun")
        public AjaxResult selectGun(@RequestParam(value = "name", required = false) String gunName) {
            List<Gun> selectGun = gunService.selectGun(gunName);
            return AjaxResult.success(selectGun);
        }

        @ApiOperation(value = "新增枪械")
        @PostMapping("addGun")
        public AjaxResult addGun(@RequestBody Gun gun){
            return AjaxResult.success(gunService.addGun(gun));
        }

        @ApiOperation(value = "修改枪械")
        @PutMapping("updateGun")
        public AjaxResult updateGun(@RequestBody Gun gun){
            return AjaxResult.success(gunService.updateGun(gun));
        }

        @ApiOperation("删除枪械")
        @ApiImplicitParam(name = "id", value = "枪械ID", required = true, dataType = "int", paramType = "path", dataTypeClass = Long.class)
        @DeleteMapping("/{id}")
        public AjaxResult deleteGun(@PathVariable int id) {
            return AjaxResult.success(gunService.deleteGun(id));
        }

        @ApiOperation("日志")
        @PostMapping("selectGun2")
        public AjaxResult selectGun3(@RequestBody Gun gun){
            return AjaxResult.success(gunService.selectGun2(gun));
        }

    }

